
class CCollitionEditor {
	private static myself:CCollitionEditor
	private stage:egret.Stage
	private collitionshape:CCOLLISION
	private scalassist:COLLITIONSCALASSIST
	private moveassist:COLLITIONMOVEASSIST
	chooseshap:egret.Shape
	private setcollition:boolean=false
	choosetool1:choosetool=choosetool.choose;
	public constructor() {
		this.stage=egret.MainContext.instance.stage
	}
	public static get editor():CCollitionEditor
	{
		if(this.myself==undefined)this.myself=new CCollitionEditor
		return this.myself
	}
    public get state()
	{
		return this.setcollition
	}
	public set state(a:boolean)
	{
		this.setcollition=a;
	}
	public changegoalpos(x:number,y:number)
	{
		 for(let a=0;a<this.collitionshape.shapepoint.length;a++)
		 {
            this.collitionshape.shapepoint[a].x+=x;
		    this.collitionshape.shapepoint[a].y+=y;
		 }
		 
	}
	public changegoalshape()
	{
		switch(this.collitionshape.shapepoint.length)
		{
			case 3:{
				this.collitionshape.setshape(this.collitionshape.shapepoint[0],this.collitionshape.shapepoint[1],this.collitionshape.shapepoint[2])
			}break;
			case 4:{
				this.collitionshape.setshape(this.collitionshape.shapepoint[0],this.collitionshape.shapepoint[1],this.collitionshape.shapepoint[2],this.collitionshape.shapepoint[3])
			}break;
			case 5:{
				this.collitionshape.setshape(this.collitionshape.shapepoint[0],this.collitionshape.shapepoint[1],this.collitionshape.shapepoint[2],this.collitionshape.shapepoint[3],this.collitionshape.shapepoint[4])
			}break;
		}
	}
	public get goal():CCOLLISION
	{
		
		return this.collitionshape
	}
	public set goal(c:CCOLLISION)
	{
	  
		this.collitionshape=c
		CMainEditor.editor.rightupdate();
		this.update()

		
	}
    public movecollition()
	{
		    this.choosetool1=choosetool.move
		    this.chackchange()
		    let b=this.goal
			if(b==undefined)return

			let stagepoint=b.parent.localToGlobal(b.shape.x,b.shape.y)
		
            if(this.moveassist==undefined)this.moveassist=new COLLITIONMOVEASSIST //第一次创建
	        this.stage.addChild(this.moveassist); //添加进舞台
	     	let a=this.moveassist.offsetxy;       //得到上下两个箭头的自身宽度偏离值
		                                          //计算y的偏移值 底部对齐式
		    let offsety=stagepoint.y-this.moveassist.height+b.shape.height+a.x
		
	    	this.moveassist.x=stagepoint.x-a.x;          
		    this.moveassist.y=offsety;
       
	}
	public chackchange()
	{
		if(this.stage.getChildIndex(this.moveassist)!=-1)
		{
			this.stage.removeChild(this.moveassist);
		}
		if(this.stage.getChildIndex(this.scalassist)!=-1)
		{
			this.stage.removeChild(this.scalassist);
		}
		if(this.stage.getChildIndex(this.chooseshap)!=-1)
		{
			this.stage.removeChild(this.chooseshap);
		}
	}
	public scalcollition()
	{
		this.choosetool1=choosetool.scal
		 this.chackchange()
		let b=this.goal
		if(b==undefined)return
		if(this.scalassist==undefined)this.scalassist=new COLLITIONSCALASSIST //第一次创建
		this.stage.addChild(this.scalassist);
       	if(this.goal.judgetype==judgetype.circle)
		{
            this.scalassist.circleassist();
		}
		else if(this.goal.judgetype==judgetype.rectangle)
		{
           this.scalassist.rectassist();
		}
		else
		{
            switch(this.collitionshape.shapepoint.length)
		    {
				case 3:
				{
                  this.scalassist.threepointassist();
				}break;
				case 4:
				{
                  this.scalassist.fourpointassist();
				}break;
				case 5:
				{
                 this.scalassist.fivepointassist();
				}break;
		    }
		}
	}
	private drawchooseshape()
	{
		 this.chooseshap.graphics.clear();
	     this.chooseshap.graphics.lineStyle(5)
		if(this.collitionshape.judgetype!=judgetype.circle)
		{
	     this.chooseshap.x=0;
		 this.chooseshap.y=0;
          for(let a=0;a<this.goal.shapepoint.length;a++)
	      {
			 
			  let parent=this.collitionshape.parent
			 // console.log("parent",parent);
              let ax=this.collitionshape.shapepoint[a].x
			  let ay=this.collitionshape.shapepoint[a].y
			 // console.log("sdf",ax,ay);
		  let stagepoint=parent.localToGlobal(ax,ay);
	   //	  console.log("1234",stagepoint.x,stagepoint.y);
		  this.chooseshap.graphics.lineTo(stagepoint.x,stagepoint.y)
	      }
	      let stagepoint=this.collitionshape.parent.localToGlobal(this.collitionshape.shapepoint[0].x,this.collitionshape.shapepoint[0].y);
	      this.chooseshap.graphics.lineTo(stagepoint.x,stagepoint.y)
	
	     
		}
		else{
                let r2=this.collitionshape.r
			    let stagepoint=this.collitionshape.parent.localToGlobal(this.collitionshape.shapepoint[0].x,this.collitionshape.shapepoint[0].y);
			  
			   this.chooseshap.graphics.drawRect(0,0,r2*2,r2*2)
			   this.chooseshap.x=stagepoint.x-r2
			   this.chooseshap.y=stagepoint.y-r2
		}
		 this.chooseshap.graphics.endFill();
	}
	public choosecollition()
	{
		this.choosetool1=choosetool.choose
		if(this.chooseshap==undefined)this.chooseshap=new egret.Shape
		this.chackchange();
		let b=this.goal
		if(b==undefined)return
		let stagepoint=b.parent.localToGlobal(b.shape.x,b.shape.y)
        this.drawchooseshape();
		this.stage.addChild(this.chooseshap)
        
	}
	public update() //更新选项
	{
		if(!CCollitionEditor.editor.state)
		{
			this.chackchange();
			return;
		}
		else if(this.choosetool1==choosetool.move)
	      this.movecollition();
		else if(this.choosetool1==choosetool.scal)
		  this.scalcollition();
		else if(this.choosetool1==choosetool.choose)
		{
			this.choosecollition();
		}
	}
	public collitiondeletes()
	{
      this.goal.parent.removejudgebox(this.goal.name);
	  this.goal.parent.removeunjudgebox(this.goal.name);
	  ASSIST.getASSIST.removeoutbodycollsion(this.goal);
	  CMainEditor.editor.collitionnamechange();
	  this.goal=undefined;
	  this.update();
	}
}